<?php



if (!is_null($_GET["name"])) {
	$name = $_GET["name"];
}

if (!is_null($_GET["age"])) {
	echo "age不为空,赋值<br>";
	$age = $_GET["age"];
} else {
	echo "age为空<br>";
}

if (!is_null($_GET["email"])) {
	$email = $_GET["email"];
}

// 正则

// exit();

echo "输出 name = " . $name . "<br>age = " . $age . "<br>email = " . $email . "<br>";

// 数据库连接
$con = mysqli_connect("localhost","root","root");
if (!$con) {
  die('Could not connect: ' . mysql_error());
} else {
	echo '数据库连接成功';
}

// 选择哪个数据库
$ret = mysqli_select_db($con, "mymessageboard");
if ($ret) {
	echo "<br>有这个数据库";
} else {
	echo "<br>没有这个数据库";
}

// 创建数据库
// if (mysqli_query($con, "CREATE DATABASE mymessageboard")) {
// 	echo "数据库创建成功";
// } else {
// 	echo "创建失败: " . mysqli_error() . "<br>";
// }

// 建表
// $sql = "create table Persons
// (
// id int(11) NOT NULL AUTO_INCREMENT, 
// PRIMARY KEY(id),
// name varchar(15),
// age int(3),
// email varchar(50)
// )";
// $ret = mysqli_query($con, $sql);
// if ($ret) {
// 	echo "建表成功";
// } else {
// 	echo "创建失败: " . mysqli_error() . "<br>";
// }

// 	$sql = "update Persons set age = " . $age . ", name = '" . $name ."', email = '" . $email . "' where id = " . $_GET["id"];
// 	echo "$sql";

// exit();

// 修改数据
$id = $_GET["id"];
$id = (int)$id;

// 如果有id，说明是修改数据
if ($id > 0) {
	// 修改数据
	$sql = "update Persons set age = " . $age . ", name = '" . $name ."', email = '" . $email . "' where id = " . $_GET["id"];
	echo "$sql";
	$ret = mysqli_query($con, $sql);
	if ($ret) {
		echo "更新成功<br/>";
	} else {
		echo "更新失败: " . mysqli_error() . "<br>";
	}
} else {
	// 插入数据
	$sql = "insert into Persons (name, age, email) VALUES ('".$name."',".$age.",'" . $email . "')";
	echo "$sql";
	$ret = mysqli_query($con, $sql);
	if ($ret) {
		echo "插入成功<br/>";
	} else {
		echo "插入失败: " . mysqli_error() . "<br>";
	}
}



// 
// $sql = "select * from user where id = 1";
// $ret = mysqli_query($con, $sql);

// $ret = mysqli_fetch_array($ret);
// echo "<br>" . $ret["name"] . "<br>";
// print_r($ret);
// var_dump($ret);

mysqli_close($con);

